package com.ghsc.ghxc.jsb.ui.login

import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.utils.KeyboardUtils
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityUpPwdBinding

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.login.activity
 * 类名：RetrievePwdActivity
 * 创建时间：2023/4/7 16:39
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 修改登录密码
 */
class RetrievePwdActivity  : BaseVMActivity<LoginViewModel, ActivityUpPwdBinding>() {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_up_pwd
    }

    override fun attachVMClass(): Class<LoginViewModel>? {
        return LoginViewModel::class.java
    }
    var myCountDownTimer: MyCountDownTimer? = null

    var bundle : Bundle?=null
    var sign:Int=1


    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    override fun initView() {
        KeyboardUtils.fixAndroidBug5497(this)
        bundle=intent.extras
        sign=bundle!!.getInt("sign",1)


        binding.bar.topTitle.text="修改登录密码"
        binding.bar.topLeftView.setSingleClickListener {
            hideKeyboard(binding.etPhone)
            finish()
        }



        var userInfo = UserManager.getUserInfo()
        userInfo?.let {
            binding.etPhone.setText(it.mobile)
//            binding.etPhone.isFocusable = true
        }

    }


    override fun initClickListener() {
        super.initClickListener()
        binding.tvBtnCode.setSingleClickListener {
            //验证码
            if (TextUtils.isEmpty(binding.etPhone.text)){
                showToast("请输入手机号")
                return@setSingleClickListener
            }
            if (binding.etPhone.text.toString().trim().length != 11) {
                showToast("手机格式错误")
                return@setSingleClickListener
            }
            val map: HashMap<String, Any> = HashMap()
            if (sign==1){
                map["mobile"] = binding.etPhone.text.toString().trim()
                map["type"]=2
            }
            GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onPhoneCodeMethod(it1) }
        }
        binding.btnNext.setSingleClickListener {
            // 修改
            if (TextUtils.isEmpty(binding.etPhone.text)){
                showToast("请输入手机号")
                return@setSingleClickListener
            }
            if (TextUtils.isEmpty(binding.etLoginPwd.text)){
                showToast("请输入新密码")
                return@setSingleClickListener
            }
            if (binding.etLoginPwd.text.toString().trim().length < 6) {
                showToast("密码错误，不得少于6位")
                return@setSingleClickListener
            }
            if (TextUtils.isEmpty(binding.etLoginPwdTo.text)){
                showToast("请确认新密码")
                return@setSingleClickListener
            }
            if (binding.etLoginPwd.text.toString().trim()!= binding.etLoginPwdTo.text.toString().trim()){
                showToast("两次密码不一致")
                return@setSingleClickListener
            }
            // 修改密码
            val map: HashMap<String, Any> = HashMap()
            map["mobile"] = binding.etPhone.text.toString().trim()
            map["password"] = binding.etLoginPwd.text.toString().trim()
            map["smscode"] = binding.etCode.text.toString().trim()
            GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onSetUpPwdMethod(it1) }

        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@RetrievePwdActivity) {
                showToast( it)
            }
            againLoginMsg.observe(this@RetrievePwdActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onSetUpPwdData.observe(this@RetrievePwdActivity) {
                showToast("修改成功")
                finish()
            }
            onPhoneCodeData.observe(this@RetrievePwdActivity) {
                if (myCountDownTimer==null)
                    myCountDownTimer = MyCountDownTimer(60000, 1000)
                showToast("验证码发送成功")
                myCountDownTimer?.start()
            }

        }
    }
    /**
     * 倒计时函数
     */
    inner class MyCountDownTimer(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {
        //计时过程
        override fun onTick(l: Long) {
            //防止计时过程中重复点击
            binding.tvBtnCode.isClickable = false
            binding.tvBtnCode.text = (l / 1000).toString() + "s"
        }

        //计时完毕的方法
        override fun onFinish() {
            //设置可点击
            binding.tvBtnCode.text = resources.getString(R.string.time_again_str)
            binding.tvBtnCode.isClickable = true
        }
    }
}